<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\Cache;

/*------------公用函数start----------------------*/
/**
 * 将字节转换为可读文本
 * @param int $size 大小
 * @param string $delimiter 分隔符
 * @return string
 */
if (!function_exists('format_bytes')) {
    function format_bytes($size, $delimiter = '')
    {
        $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
        for ($i = 0; $size >= 1024 && $i < 6; $i++)
            $size /= 1024;
        return round($size, 2) . $delimiter . $units[$i];
    }

}
/**
 * @param $code 返回码
 * @param $data 返回数据主题
 * @param $msg  返回消息
 */
if (!function_exists('json_return')) {
    function json_return($code, $msg, $data = null)
    {
        exit(json_encode(array('ret' => $code, 'msg' => $msg, 'data' => $data,)));
    }
}
/**
 * curl 请求
 */
function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
    // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
    //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
   // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    curl_close($curl);

    return $res;
}

/**
 * php将字符串分割成数组实现中文分词
 */
function math($string,$code ='UTF-8'){
    if ($code == 'UTF-8') {
        $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
    } else {
        $pa = "/[\x01-\x7f]|[\xa1-\xff][\xa1-\xff]/";
    }
    preg_match_all($pa, $string, $t_string);
    $math=[];
    foreach($t_string[0] as $k=>$s){
        $math[]=$s;
    }
    return $math;
}

/**
 * 检测是后台登入
 */
function isAdminLogin(){
    $user = session('admin_login');
    if (empty($user)) {
        return 0;
    } else {
        return session('admin_login_sign') == data_auth_sign($user) ? $user['uid'] : 0;
    }
}

/**
 * sha1签名
 */
function data_auth_sign($data) {
    //数据类型检测
    if(!is_array($data)){
        $data = (array)$data;
    }
    ksort($data); //排序
    $code = http_build_query($data); //url编码并生成query字符串
    $sign = sha1($code); //生成签名
    return $sign;
}

/**
 * 判断时间戳是不是为0
 */
function isTimeZero($time){
    if(strtotime($time)>0){
        return true;
    }else{
        return false;
    }
}

/**
 * 检查是否微信
 */
function is_weixin() {
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
        return true;
    } return false;
}

/**
 * web检查
 */
function web_check()
{
    $uri  = url('login/login');
    $uri0 = url('login/bang');
    $uri1 = url('index/index');
    $uid  = session('uid');
    if (!$uid) {
        if (!is_weixin()) {
            echo "<script>if(confirm('您还没有登陆,确定前往登陆？')){
                   location=\" " . $uri . "\";
                 }else{
                  location=\" " . $uri1 . "\";
                 }</script>";
        }else{
            echo "<script>if(confirm('您还没有绑定手机,前往绑定？')){
                   location=\" " . $uri0 . "\";
                 }else{
                  location=\" " . $uri1 . "\";
                 }</script>";

        }
    }

}

/**
 * 判断客服端是否为ｉｏｓ
 */
function is_ios(){
    if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
           return 2;
    }else{
           return 1;
    }
}

/**
 * curl POST
 * @param $curlPost
 * @param $url
 * @return mixed
 */
function curlPost($curlPost,$url){

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);

    curl_setopt($curl, CURLOPT_HEADER, false);

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($curl, CURLOPT_NOBODY, true);

    curl_setopt($curl, CURLOPT_POST, true);

    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);

    $return_str = curl_exec($curl);

    curl_close($curl);

    return $return_str;

}

/**
 * 返回随机整数
 * @param int $length
 * @param int $numeric
 * @return string
 *
 */
function random($length = 6 , $numeric = 0) {

    PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
    if($numeric) {

        $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));

    } else {
        $hash = '';
        $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
        $max = strlen($chars) - 1;
        for($i = 0; $i < $length; $i++) {

            $hash .= $chars[mt_rand(0, $max)];

        }
    }
    return $hash;

}

/**
 * 根据ip获取城市
 */
function get_city_id($ip){
//    if(empty($ip)){
//        $ip=get_client_ip();
//    }
//    $url='http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
//    $result = file_get_contents($url);
//    $result = json_decode($result,true);
//    if($result['code']!==0) {
//        return false;
//    }
    $result = "本地";
    return $result;
}

/**
 * 系统非常规MD5加密方法
 * @param  string $str 要加密的字符串
 * @return string
 */
function data_md5($str, $key = 'PADMIN')
{
    return '' === $str ? '' : md5(sha1($str) . $key);
}

/**
 * 使用上面的函数与系统加密KEY完成字符串加密
 * @param  string $str 要加密的字符串
 * @return string
 */
function data_md5_key($str, $key = '')
{

    if (is_array($str)) {

        ksort($str);

        $data = http_build_query($str);

    } else {

        $data = (string) $str;
    }
    return empty($key) ? data_md5($data, \think\Config::get('API_KEY')) : data_md5($data, $key);
}

/**
 * 获取手机类型
 */
function get_mobile_vision()
{
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    if (stripos($user_agent, "iPhone") !== false) {
        $brand = 'iPhone';
    } else if (stripos($user_agent, "SAMSUNG") !== false || stripos($user_agent, "Galaxy") !== false || strpos($user_agent, "GT-") !== false || strpos($user_agent, "SCH-") !== false || strpos($user_agent, "SM-") !== false) {
        $brand = '三星';
    } else if (stripos($user_agent, "Huawei") !== false || stripos($user_agent, "Honor") !== false || stripos($user_agent, "H60-") !== false || stripos($user_agent, "H30-") !== false) {
        $brand = '华为';
    } else if (stripos($user_agent, "Lenovo") !== false) {
        $brand = '联想';
    } else if (strpos($user_agent, "MI-ONE") !== false || strpos($user_agent, "MI 1S") !== false || strpos($user_agent, "MI 2") !== false || strpos($user_agent, "MI 3") !== false || strpos($user_agent, "MI 4") !== false || strpos($user_agent, "MI-4") !== false) {
        $brand = '小米';
    } else if (strpos($user_agent, "HM NOTE") !== false || strpos($user_agent, "HM201") !== false) {
        $brand = '红米';
    } else if (stripos($user_agent, "Coolpad") !== false || strpos($user_agent, "8190Q") !== false || strpos($user_agent, "5910") !== false) {
        $brand = '酷派';
    } else if (stripos($user_agent, "ZTE") !== false || stripos($user_agent, "X9180") !== false || stripos($user_agent, "N9180") !== false || stripos($user_agent, "U9180") !== false) {
        $brand = '中兴';
    } else if (stripos($user_agent, "OPPO") !== false || strpos($user_agent, "X9007") !== false || strpos($user_agent, "X907") !== false || strpos($user_agent, "X909") !== false || strpos($user_agent, "R831S") !== false || strpos($user_agent, "R827T") !== false || strpos($user_agent, "R821T") !== false || strpos($user_agent, "R811") !== false || strpos($user_agent, "R2017") !== false) {
        $brand = 'OPPO';
    } else if (strpos($user_agent, "HTC") !== false || stripos($user_agent, "Desire") !== false) {
        $brand = 'HTC';
    } else if (stripos($user_agent, "vivo") !== false) {
        $brand = 'vivo';
    } else if (stripos($user_agent, "K-Touch") !== false) {
        $brand = '天语';
    } else if (stripos($user_agent, "Nubia") !== false || stripos($user_agent, "NX50") !== false || stripos($user_agent, "NX40") !== false) {
        $brand = '努比亚';
    } else if (strpos($user_agent, "M045") !== false || strpos($user_agent, "M032") !== false || strpos($user_agent, "M355") !== false) {
        $brand = '魅族';
    } else if (stripos($user_agent, "DOOV") !== false) {
        $brand = '朵唯';
    } else if (stripos($user_agent, "GFIVE") !== false) {
        $brand = '基伍';
    } else if (stripos($user_agent, "Gionee") !== false || strpos($user_agent, "GN") !== false) {
        $brand = '金立';
    } else if (stripos($user_agent, "HS-U") !== false || stripos($user_agent, "HS-E") !== false) {
        $brand = '海信';
    } else if (stripos($user_agent, "Nokia") !== false) {
        $brand = '诺基亚';
    } else {
        $brand = '其他手机';
    }

    return $brand;
}

/**
 * 检查银行卡
 */
function is_card($s){
    $n = 0;
    $ns = strrev($s); // 倒序
    for ($i=0; $i <strlen($s) ; $i++) {
        if ($i % 2 ==0) {
            $n += $ns[$i]; // 偶数位，包含校验码
        }else{
            $t = $ns[$i] * 2;
            if ($t >=10) {
                $t = $t - 9;
            }
            $n += $t;
        }
    }
    return ( $n % 10 ) == 0;
}

/**
 * 检查身份证号码
 */
function is_id_card($number) {
    //检查是否是身份证号
    // 转化为大写，如出现x
    $number = strtoupper($number);
    //加权因子
    $wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
    //校验码串
    $ai = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
    //按顺序循环处理前17位
    $sigma = 0;
    for($i = 0;$i < 17;$i++){
        //提取前17位的其中一位，并将变量类型转为实数
        $b = (int) $number{$i};      //提取相应的加权因子
        $w = $wi[$i];     //把从身份证号码中提取的一位数字和加权因子相乘，并累加
        $sigma += $b * $w;
    }
    //计算序号
    $snumber = $sigma % 11;
    //按照序号从校验码串中提取相应的字符。
    $check_number = $ai[$snumber];
    if($number{17} == $check_number){
        return true;
    }else{
        return false;
    }
}

/**
 * 将xml转化为数组
 * @param $xml
 * @return mixed
 */
function xml_to_array($xml){

    $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";

    if(preg_match_all($reg, $xml, $matches)){

        $count = count($matches[0]);

        for($i = 0; $i < $count; $i++){

            $subxml= $matches[2][$i];

            $key = $matches[1][$i];

            if(preg_match( $reg, $subxml )){

                $arr[$key] = xml_to_array( $subxml );

            }else{

                $arr[$key] = $subxml;

            }

        }

    }

    return $arr;

}

/**
 * 添加流水记录
 */
function addDetail($uid,$money,$exp,$red_id=0){
    if(in_array($exp,[1,4,6])){
        $type=1;
    }else{
        $type=2;
    }
    $balance = \think\Db::name('member')->where('id',$uid)->value('money');
    $data=[
        'uid'    =>$uid,
        'type'   =>$type,
        'money'  =>$money,
        'balance'=>$balance,
        'exp'    =>$exp,
        'red_id' =>$red_id
    ];
   \think\Db::name('detail')->insert($data);
}

/**
 * 计算连续签到天数
 */
function continue_day($day_list)
{
    //昨天开始时间戳
    $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));

    if($beginYesterday>$day_list[0]) $days = 0;
    else $days = 1;

    $count = count($day_list);
    for($i=0;$i<$count;$i++){
        if($i<$count-1){
            $res = compareDay($day_list[$i],$day_list[$i+1]);
            if($res) $days++;
            else break;
        }
    }

    return $days;
}

function compareDay($curDay,$nextDay)
{
    $lastBegin = mktime(0,0,0,date('m',$curDay),date('d',$curDay)-1,date('Y',$curDay));
    $lastEnd   = mktime(0,0,0,date('m',$curDay),date('d',$curDay),date('Y',$curDay))-1;

    if($nextDay>=$lastBegin && $nextDay<=$lastEnd){
        return true;
    }else{
        return false;
    }

}

/**
 * @param $money_total
 * @param $num
 * @return array
 * 分裂红包算法
 */
function send_red($money_total,$num) {
    $rand_arr = array();
    for($i=0; $i<$num; $i++) {
        $rand = rand(1, 100);
        $rand_arr[] = $rand;
    }
    $rand_sum = array_sum($rand_arr);
    $rand_money_arr = array();
    $rand_money_arr = array_pad($rand_money_arr, $num, 0.01);  //保证每个红包至少0.01
    foreach ($rand_arr as $key => $r) {
        $rand_money = number_format($money_total*$r/$rand_sum, 2);
        if($rand_money <= 0.01 || number_format(array_sum($rand_money_arr), 2,'.','') >= number_format($money_total, 2,'.','')) {
            $rand_money_arr[$key] = 0.01;
        } else {
            $rand_money_arr[$key] = $rand_money;
        }

    }
    $max_index = $max_rand = 0;
    foreach ($rand_money_arr as $key => $rm) {
        if($rm > $max_rand) {
            $max_rand = $rm;
            $max_index = $key;
        }
    }
    unset($rand_money_arr[$max_index]);
    //这里的array_sum($rand_money_arr)一定是小于$money_total的
    $rand_money_arr[$max_index] = number_format($money_total - array_sum($rand_money_arr), 2);
    ksort($rand_money_arr);
    return $rand_money_arr;
}

/**
 * 添加个人消息
 */
function add_personal_msg($uid,$content){
     $insert['uid']      = $uid;
     $insert['content']  = $content;
     $insert['create_at'] = time();
     \think\Db::name('member_msg')->insert($insert);
}

/**
 * 加入群主
 */
function add_group($uid,$group_id,$is_owner=0){
    $insert = [
        'uid' => $uid,
        'group_id' => $group_id,
        'is_owner' => $is_owner
    ];
    \think\Db::name('groups_ship')->insert($insert);
}





